package com.thb.business.dao.Impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.thb.business.dao.IBookDao;
import com.thb.business.model.Book;
import com.thb.business.model.BookColumn;

@Repository
public class BookColumnDaoImpl implements IBookDao {

	@Autowired
	private JdbcTemplate jTemplate;

	@Override
	public List<Book> findAll(BookColumn para) {
		String sql = "select * from t_book a join t_book_column b on a.id = b.book_id where b.column_id = ? order by a.id limit ?,? ";
		Object[] arg = new Object[] {para.getColumnId(),(para.getPage()-1)*para.getPageSize(),para.getPageSize()};
		
		return jTemplate.query(sql, arg,new RowMapper<Book>() {
			public Book mapRow(ResultSet rs, int no) throws SQLException {

				return jsonMapper(rs, no);
			}
		});
	}
	
	@Override
	public int countAll(BookColumn para) {
		
		String sql = "select count(*) from t_book a join t_book_column b on a.id = b.book_id where b.column_id = ? ";
		Object[] arg = new Object[] {para.getColumnId()};
		
		return jTemplate.queryForInt(sql, arg);
	}

	private Book jsonMapper(ResultSet rs, int no) throws SQLException {

		Book vo = new Book();
		vo.setId(rs.getInt("id"));
		vo.setBookName(rs.getString("book_name"));
		vo.setDesc(rs.getString("desc"));
		vo.setLogo(rs.getString("logo"));
		vo.setBookCount(rs.getInt("book_count"));
		
		return vo;
	}

	

}
